﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SMM.TipsForm;

namespace SMM.MainForm
{
    public partial class CashForm : Form
    {
        int n = 1;
        String ID = null;
        String Name = null;
        sqlHelper helper = new sqlHelper();
        setListForm setList = null;
        int flag = 0;
        public CashForm(String ID)
        {
            InitializeComponent();
            this.ID = ID;
            Name = helper.getUserNameByID(ID);
        }

        private void CashForm_Load(object sender, EventArgs e)
        {
            TSSLID.Text = "收银员ID："+ID;
            TSSLName.Text = "收银员姓名：" + Name;
            allPriceLable.Text = "0￥";
            allNumLabel.Text = "0";
            reload();
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TSSLTime.Text = "当前时间：" + DateTime.Now.ToString("yyyy - MM - dd hh: mm: ss");
            if (flag == 1 && setList.IsDisposed) {
                while (dgvList.Rows.Count != 1)
                {
                    dgvList.Rows.RemoveAt(0);
                }
                allPriceLable.Text = "0￥";
                allNumLabel.Text = "0";
                reload();
                flag = 0;
            }
        }

        private void LogoutBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            LoginForm f = new LoginForm();
            f.ShowDialog();
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            if (helper.cargoExist(barcodeEdit.Text))
            {
                cargoNameLable.Text = helper.getCargoInfoByBar(barcodeEdit.Text, "Name");
                stockLable.Text = helper.getCargoInfoByBar(barcodeEdit.Text, "Stock");
                cargoPriceLable.Text = helper.getCargoInfoByBar(barcodeEdit.Text, "Price")+"";
            }
            else {
                MessageBox.Show("商品不存在！","错误");
            }
            try
            {
                oneSumLable.Text = int.Parse(cargoNum.Text) * float.Parse(cargoPriceLable.Text) + "";
            }
            catch (Exception exc) {
                oneSumLable.Text = 0+"";
            }
        }

        private void submitBtn_Click(object sender, EventArgs e)
        {
            int index = dgvList.Rows.Add();
            dgvList.Rows[index].Cells[0].Value = n++;
            dgvList.Rows[index].Cells[1].Value = barcodeEdit.Text;
            dgvList.Rows[index].Cells[2].Value = helper.getCargoInfoByBar(barcodeEdit.Text, "Name"); ;
            dgvList.Rows[index].Cells[3].Value = helper.getCargoInfoByBar(barcodeEdit.Text, "Price");
            dgvList.Rows[index].Cells[4].Value = cargoNum.Text;
            dgvList.Rows[index].Cells[5].Value = float.Parse(helper.getCargoInfoByBar(barcodeEdit.Text, "Price"))*int.Parse(cargoNum.Text);
            reload();
        }

        private void settlementBtn_Click(object sender, EventArgs e)
        {
            String str = "";
            for (int i = 0; i < dgvList.RowCount; i++)
            {
                for (int j = 0; j < dgvList.ColumnCount; j++)
                {
                    if (j ==1) continue;
                    str+=Convert.ToString(dgvList.Rows[i].Cells[j].Value).Trim()+"\t";
                }
                str += "\r\n";
            }
            setList = new setListForm(Name,str,int.Parse(allNumLabel.Text),allPriceLable.Text);
            setList.Show();
            n = 1;
            flag = 1;
        }

        private void cargoNum_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                oneSumLable.Text = int.Parse(cargoNum.Text) * float.Parse(cargoPriceLable.Text) + "";
            }
            catch (Exception exc) {
                oneSumLable.Text = "0";
            }
        }

        private void reload()
        {
            barcodeEdit.Text = "";
            cargoNameLable.Text = "--";
            stockLable.Text = "";
            cargoPriceLable.Text = "";
            oneSumLable.Text = "";
            cargoNum.Text = 1+"";
            barcodeEdit.Select();
            allNumLabel.Text = Math.Ceiling(getCluSum(4))+"";
            allPriceLable.Text = getCluSum(5)+"￥";
        }

        private float getCluSum(int cnum) {
            float sum = 0;
            if (dgvList.Rows.Count == 1) return 0;
            for (int i = 0; i < dgvList.RowCount-1; i++) {
                sum += float.Parse(dgvList.Rows[i].Cells[cnum].Value + "");
            }
            return sum;
        }
    }
}
